// RuleJudge.Judge.NGU
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using System.Windows.Forms;
using RuleJudge.Judge;
using RuleJudge.RuleData;
using SLANCommon.Enum;

public class NGU : BaseJudge
{
    public NGU()
    {
        Type = EProjectType.AQ;
        base.TubesNo = new List<int> { 1, 1, 1, 1, 2, 2, 2 };
        base.ChannelsNo = new List<int> { 1, 2, 3, 4, 1, 2, 4 };
    }

    int FAM = 0;
    int VIC = 1;
    int ROX = 2;
    int CY5 = 3;

    bool NC_Check_Pass = false;
    bool PC_Check_Pass = false;
    bool Leak_NC_Control = false;
    bool Leak_PC_Control = false;

    const double CT_Cutoff = 34.0;  // 沙眼衣原体（CT）
    const double NG_Cutoff = 35.0;  // 淋病奈瑟菌（NG）
    const double UU_Cutoff = 34.0;  // 解脲脲原体(UU)
    const double MH_Cutoff = 33.0;  // 人型支原体(MH)
    const double MG_Cutoff = 33.0;  // 生殖支原体（MG）
    const double IC_Cutoff = 35.0;  // 内标IC-A, IC-B


    // 反应管各通道检测靶标
    //      0         1        2       3
    //  |------------------------------------
    //  |   FAM  |   VIC  |   ROX  |   CY5  |
    //  |   CT   |   NG   |   UU   |   IC-A |
    //  |-----------------------------------|
    //  |   FAM  |   VIC  |   ROX  |   CY5  |
    //  |   MH   |   MG   |    /   |   IC-B |
    //  |-----------------------------------|

    // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
    // 开始因阳性质控、待测(未知)样本判读
    public override string Judge(List<Sample> samples)
    {
        if (samples.Count == 0)
        {
            return "无判断样本！";
        }

        #region 阴性指控判断
        List<Sample> NC_list = samples.Where((Sample s) => s.Type == SampleType.Negative).ToList();
        if (NC_list.Count != 0)
        {
            foreach (Sample NC in NC_list)
            {
                NC_Check_Pass = ((NC.Tubes[0].Targets[FAM].Result.Ct > CT_Cutoff && NC.Tubes[0].Targets[VIC].Result.Ct > NG_Cutoff && NC.Tubes[0].Targets[ROX].Result.Ct > UU_Cutoff && NC.Tubes[0].Targets[CY5].Result.Ct <= IC_Cutoff) &&
                                    (NC.Tubes[1].Targets[FAM].Result.Ct > MH_Cutoff && NC.Tubes[1].Targets[VIC].Result.Ct > MG_Cutoff && NC.Tubes[1].Targets[2].Result.Ct <= IC_Cutoff));
                NC.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = NC_Check_Pass ? "阴性质控合格" : "阴性质控不合格",
                    Color = NC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = NC_Check_Pass ? Colors.Red : Colors.Blue,
                });
                NC.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = NC_Check_Pass ? "阴性质控合格" : "阴性质控不合格",
                    Color = NC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = NC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = NC_Check_Pass ? Colors.Red : Colors.Blue,
                });
            }
        }
        else
        {
            Leak_NC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 阳性质控判读
        List<Sample> PC_list = samples.Where((Sample s) => s.Type == SampleType.HighPositive).ToList();
        if (PC_list.Count != 0)
        {
            foreach (Sample PC in PC_list)
            {
                PC_Check_Pass = ((PC.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff && PC.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff && PC.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff) &&
                                    (PC.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff && PC.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff));
                PC.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = PC_Check_Pass ? "阳性质控合格" : "阳性质控不合格",
                    Color = PC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = PC_Check_Pass ? Colors.Red : Colors.Blue,
                });
                PC.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = PC_Check_Pass ? "阳性质控合格" : "阳性质控不合格",
                    Color = PC_Check_Pass ? Colors.Green : Colors.Red,
                    BackgroundColor = PC_Check_Pass ? Colors.LightGreen : Colors.LightYellow,
                    ForegroundColor = PC_Check_Pass ? Colors.Red : Colors.Blue,
                });
            }
        }
        else
        {
            Leak_PC_Control = true;
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 待测样本判读
        List<Sample> UnknownSample_list = samples.Where((Sample s) => s.Type == SampleType.Unknown).ToList();
        foreach (Sample Unknown_Sample in UnknownSample_list)
        {
            // 样本内参是否合格:（1）反应孔有非内参靶标阳性；（2）反应孔靶标均为阴性，且内参阳性。
            bool IC_Check_Pass = false;
            if ((Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ||
                Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ||
                Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ||
                Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct <= IC_Cutoff) &&
                (Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ||
                Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ||
                Unknown_Sample.Tubes[1].Targets[2].Result.Ct <= IC_Cutoff))
            {
                IC_Check_Pass = true;
            }
            if (!IC_Check_Pass)
            {
                Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                continue;
            }
            // 反应管A孔FAM通道：沙眼衣原体（CT）
            Unknown_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? "沙眼衣原体阳性" : "沙眼衣原体阴性",
                Color = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔VIC通道：淋病奈瑟菌（NG）
            Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? "淋病奈瑟菌阳性" : "淋病奈瑟菌阴性",
                Color = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔ROX通道：解脲脲原体(UU)
            Unknown_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? "解脲脲原体阳性" : "解脲脲原体阴性",
                Color = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔FAM通道：人型支原体(MH)
            Unknown_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? "人型支原体阳性" : "人型支原体阴性",
                Color = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔VIC通道：生殖支原体（MG）
            Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ? "生殖支原体阳性" : "生殖支原体阴性",
                Color = Unknown_Sample.Tubes[1].Targets[VICM].Result.Ct <= MG_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        // 😹🐱‍🐉🐵🐶🐺🐱🦁🐯🦒🦊🦝🐮🐷🐗🐭🐹🐰🐻🐨🐼🐸🦓🐴🦄🐔🐲🐒🐕‍🦺🐫🦘🦙🐏🐁🐘🦨🦡🦛🦏🐪🐇🦔🐀🐿🦎🐊🐢🐍🐋🐬🐉🐟🐠🐡🦐🦑🐙🦞🦀🦜🦆🐓🦚🦉🐦🐤🦗🐜🐝🐌🦋从🕷🐞🦂🦇🦃🦅
        #region 重测样本判读
        List<Sample> RetestSample_list = samples.Where((Sample s) => s.Type == SampleType.RetestSample).ToList();
        foreach (Sample Retest_Sample in RetestSample_list)
        {
            // 样本内参是否合格:（1）反应孔有非内参靶标阳性；（2）反应孔靶标均为阴性，且内参阳性。
            bool IC_Check_Pass = false;
            if ((Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ||
                Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ||
                Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ||
                Retest_Sample.Tubes[0].Targets[CY5].Result.Ct <= IC_Cutoff) &&
                (Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ||
                Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ||
                Retest_Sample.Tubes[1].Targets[2].Result.Ct <= IC_Cutoff))
            {
                IC_Check_Pass = true;
            }
            if (!IC_Check_Pass)
            {
                Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "内参检测不合格，需重新检测！",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                    ForegroundColor = Colors.LightCoral,
                });
                continue;
            }
            // 反应管A孔FAM通道：沙眼衣原体（CT）
            Retest_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? "沙眼衣原体阳性" : "沙眼衣原体阴性",
                Color = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= CT_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔VIC通道：淋病奈瑟菌（NG）
            Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? "淋病奈瑟菌阳性" : "淋病奈瑟菌阴性",
                Color = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= NG_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管A孔ROX通道：解脲脲原体(UU)
            Retest_Sample.Tubes[0].Targets[ROX].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? "解脲脲原体阳性" : "解脲脲原体阴性",
                Color = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[0].Targets[ROX].Result.Ct <= UU_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔FAM通道：人型支原体(MH)
            Retest_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? "人型支原体阳性" : "人型支原体阴性",
                Color = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= MH_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
            // 反应管B孔VIC通道：生殖支原体（MG）
            Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
            {
                Content = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ? "生殖支原体阳性" : "生殖支原体阴性",
                Color = Retest_Sample.Tubes[1].Targets[VICM].Result.Ct <= MG_Cutoff ? Colors.Red : Colors.Green,
                BackgroundColor = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= MG_Cutoff ? Colors.LightYellow : Colors.LightYellow,
            });
        }
        #endregion

        /* *****************************************************************************************************
        * 实验中未设置阴阳性质控不影响判读，仅给出弹窗提醒缺少质控 *************************************************
        * 阴阳性质控不合格的，则给出提醒，但仍然进行判读 **********************************************************
        * *****************************************************************************************************
        */
        #region 质控结果异常（不合格，缺失）提示
        if (!NC_Check_Pass || !PC_Check_Pass)
        {
            System.Windows.Forms.MessageBox.Show("一个或多个阴阳性质控不符合要求！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        if (Leak_NC_Control || Leak_PC_Control)
        {
            System.Windows.Forms.MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        #endregion

        return string.Empty;
    }
}